import React, {Component} from 'react'

export const dataCalc=({
  beginDate='',
  endDate='',
  jobName='',
  count={
    totalNum:0,
    successNum:0,
    failNum:0
  },// 总体执行情况
  task=[],
  day=[],
  fail=[],
  jobList=[],
  details=[],
  page={
    pagesize: 0,
    currentpage: 0,
    totalpage: 0,
    totalnum: 0
  },
  dispatch,location,tag
  })=>{
  const toobarConfig={
    start:beginDate,
    end:endDate,
    format:'YYYY-MM-DD',
    dispatch,location,tag
  }
  if(jobList.length){
    // 每个任务执行情况
    const taskSuccessArr=task.map(item=>item.successNum)
    const taskFailArr=task.map(item=>item.failNum)
    taskSuccessArr.unshift('成功')
    taskFailArr.unshift('失败')
    // 每日执行记录
    const daySuccessArr=day.map(item=>item.successNum)
    const dayFailArr=day.map(item=>item.failNum)
    daySuccessArr.unshift('执行成功')
    dayFailArr.unshift('执行失败')
    // 每日失败记录
    const failArr=[]
    jobList.map((item,i)=>{
      const itemArr=fail.map(iitem=>eval(iitem[item]))
      itemArr.unshift(item)
      failArr.push(itemArr)
    })
    // 获取下拉列表数据
    const jobListArr=jobList.map(item=>{
      return {
        name:item,
        value:item
      }
    })
    jobListArr.unshift({
      name:"全部",
      value:""
    })
    // 失败列表
    details.map((item,i)=>{
      item.key=i
    })
    const failColumn=[
      {
        title:"日期",
        dataIndex:"date",
        width:'10%'
      },
      {
        title:"任务名称",
        dataIndex:"jobName",
        width:'10%'
      },
      {
        title:"开始时间",
        dataIndex:"startTime",
        width:'10%',
        render: (text, record) => <span>{text.split(" ")[0]}<br />{text.split(" ")[1]}</span>
      },
      {
        title:"结束时间",
        dataIndex:"endTime",
        width:'10%',
        render: (text, record) => <span>{text.split(" ")[0]}<br />{text.split(" ")[1]}</span>
      },
      {
        title:"运行主机",
        dataIndex:"shardingItem",
        width:'8%',
        // render: (text, record) => <span>{text.split(" ")[0]}<br />{text.split(" ")[1]}</span>
      },

      {
        title:"执行状态",
        dataIndex:"jobResult",
        width:'8%',
        render: (text, record) => text==='Fail'?<span style={{color:'red'}}>失败</span>:<span>成功</span>
      },
      {
        title:"错误详情",
        dataIndex:"failDetail"
      },
    ]


    const generalConfig={
      title:"总体情况",
      count:{
        data:[
      		{
      			name:"总计",
      			value:count.totalNum
      		},
      		{
      			name:"成功",
      			value:count.successNum
      		},
      		{
      			name:"失败",
            value:count.failNum
      		}
      	]
      },
      charts:{
    		data:{
          name:task.map(item=>item.jobName),
      		data:[taskSuccessArr,taskFailArr]
        },
        dispatch,location,tag
    	}
    }
    const executeRecordsConfig={
      title:"每日执行记录",
      charts:{
    		data:{
          name:day.map(item=>item.date),
      		data:[daySuccessArr,dayFailArr]
        }
    	}
    }
    const failRecordsConfig={
      title:"每日失败记录",
      charts:{
        data:{
      		name:fail.map(item=>item.date),
      		data:failArr
      	}
      }
    }
    const detailsConfig={
      title:"失败详情",
      select:{
        data:jobListArr,
        value:'all'
      },
      data : {
        columns:failColumn,
        data:details,
        key:"key"
      },
      page,
      beginDate,
      endDate,
      jobName,
      dispatch,location,tag
    }
    return {
      toobarConfig,
      generalConfig,
      executeRecordsConfig,
      failRecordsConfig,
      detailsConfig
    }
  }else{
    return {
      toobarConfig
    }
  }
}
